package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.utils.NotNullException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
public class DeptControllsr {
    @Autowired
    private DeptService deptService;

    @PutMapping
    public Result update(@RequestBody Dept dept){
        return Result.success(deptService.update(dept));
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        return Result.success(deptService.add(dept));
    }

    @GetMapping
    public Result findAll(){
        List<Dept> list = deptService.findALl();
        return Result.success(list);
    }

    @DeleteMapping
    public Result delete(Integer id) {
        return Result.success(deptService.delete(id));
    }


    /**
     * 根据ID删除部门 - 简单参数接收: 方式一 (HttpServletRequest)
     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idString = request.getParameter("id");
//        int id = Integer.parseInt(idString);
//        return Result.success(deptService.delete(id));
//    }

    /**
     * 根据ID删除部门 - 简单参数接收: 方式一 (HttpServletRequest)
     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//
//        System.out.println("根据ID删除部门: " + id);
//        return Result.success();
//    }

//    @DeleteMapping //通过Spring提供的 @RequestParam 注解，将请求参数绑定给方法形参
//    public Result delete(@RequestParam("id") Integer id){
//        return Result.success(deptService.delete(id));
//    }
}
